/**
 * @(#)TaskArrayAdapter.java        1.0 2/26/12
 * 
 * Copyright (c) 2012 public static droid main
 * Georgia Institute of Technology, Atlanta, GA
 * All rights reserved.
 */

package edu.gatech.oad.publicstaticdroidmain.taskmanager.ui;

import java.text.DateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import edu.gatech.oad.publicstaticdroidmain.taskmanager.model.Task;
import edu.gatech.oad.publicstaticdroidmain.taskmanager.model.TaskManager;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * An adapter class that enables arrays of Tasks to be displayed
 * in a ListView.
 * 
 * @author Daniel Hull
 */
public class TaskArrayAdapter extends ArrayAdapter<Task> {
    private final Activity context;

    private List<Task> checkedTasks;
    private Task[] tasksBeingDisplayed;
    
    public TaskArrayAdapter(Activity context) {
	super(context, R.layout.row_layout, new LinkedList<Task>());
	this.context = context;

	this.checkedTasks = new LinkedList<Task>();
	this.tasksBeingDisplayed = new Task[] { };
    }
    
    /**
     * Returns a List of the Tasks in this adapter that are currently
     * checked.
     * 
     * @return a list of checked Tasks
     */
    public List<Task> getCheckedTasks() {
	return checkedTasks;
    }
    
    /**
     * Displays the specified array of Tasks.
     * 
     * @param tasks the array of Tasks to display
     */
    public void displayTaskArray(Task[] tasks) {
	tasksBeingDisplayed = tasks;
	
	checkedTasks.clear();
	clear();

	for (Task t: tasks) {
	    add(t);
	}
    }

    /**
     * A class to hold the widgets in each row of the ListView.
     *
     */
    static class ViewHolder {
	protected TextView taskName;
	protected TextView taskCategory;
	protected TextView taskDueDate;
	protected CheckBox checkbox;
	protected ImageView icon;
    }

    /**
     * Retrieves and inflates the views used for each row of the ListView.
     */
    public View getView(int position, View convertView, ViewGroup parent) {
	Task taskAtThisPos = tasksBeingDisplayed[position];
	
	View view = null;
	
	//create a new view if Android did not give us one to reuse
	if (convertView == null) {
	    LayoutInflater inflator = context.getLayoutInflater();
	    view = inflator.inflate(R.layout.row_layout, null);
	    final ViewHolder viewHolder = new ViewHolder();
	    
	    viewHolder.taskName = (TextView) view.findViewById(R.id.rowLayoutTaskName);
	    viewHolder.taskCategory = (TextView) view.findViewById(R.id.rowLayoutTaskCategory);
	    viewHolder.taskDueDate = (TextView) view.findViewById(R.id.rowLayoutTaskDueDate);
	    viewHolder.checkbox = (CheckBox) view.findViewById(R.id.rowLayoutCheck);
	    viewHolder.icon = (ImageView) view.findViewById(R.id.rowLayoutIcon);

	    viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		    Task element = (Task) viewHolder.checkbox.getTag();

		    if (isChecked) {
			if (!checkedTasks.contains(element)) {
			    checkedTasks.add(element);
			}
		    }
		    else {
			checkedTasks.remove(element);
		    }

		    //element.setSelected(buttonView.isChecked());
		}
	    });
	    view.setTag(viewHolder);
	    viewHolder.checkbox.setTag(taskAtThisPos);

	} else { //reuse the view given to us by Android
	    view = convertView;
	    ViewHolder viewHolder = (ViewHolder)view.getTag();
	    viewHolder.checkbox.setTag(taskAtThisPos);

	    viewHolder.checkbox.setChecked(false);
	    
//	    if (checkedTasks.contains(taskAtThisPos)) {
//		viewHolder.checkbox.setChecked(true);
//	    } else {
//		viewHolder.checkbox.setChecked(false);
//	    }
	}

	ViewHolder viewHolder = (ViewHolder)view.getTag();

	boolean complete = (taskAtThisPos.isCompleted());
	if (complete) {
	    viewHolder.icon.setImageResource(R.drawable.complete);//android.R.drawable.ic_menu_add);
	} else {
	    viewHolder.icon.setImageResource(R.drawable.not_complete);//android.R.drawable.ic_delete);
	}

	LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	View rowView = inflater.inflate(R.layout.row_layout, parent, false);

	ViewHolder holder = (ViewHolder) view.getTag();
	holder.taskName.setText(taskAtThisPos.getName());
	holder.taskCategory.setText(taskAtThisPos.getCategory());
	
	DateFormat formatter = DateFormat.getDateInstance(DateFormat.MEDIUM);
	String dateString = formatter.format(taskAtThisPos.getDueDate());
	
	holder.taskDueDate.setText(dateString);

	return view;
    }
}
